package com.szeastroc.core.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szeastroc.core.base.service.impl.BaseServiceImpl;
import com.szeastroc.core.bean.system.SystemArea;
import com.szeastroc.core.mapper.system.SystemAreaMapper;
import com.szeastroc.core.service.system.SystemAreaService;

@Service
public class SystemAreaServiceImpl extends BaseServiceImpl<SystemArea> implements SystemAreaService {
	@Autowired
	SystemAreaMapper systemAreaMapper;
	
	public List<SystemArea> getProvince(){
		return systemAreaMapper.getProvince();
	}

	public List<SystemArea> getCity(Integer areaNo) {
		return systemAreaMapper.getCity(areaNo);
	}

	public List<SystemArea> getZone(Integer areaNo) {
		return systemAreaMapper.getZone(areaNo);
	}
	
	public SystemArea getSystemArea(SystemArea systemArea) {
		return systemAreaMapper.selectOne(systemArea);
	}

	public List<SystemArea> getSystemAreaByNames(String name){
		List<SystemArea> lists = new ArrayList<SystemArea>();
		String[] names = name.split(",");
		for (String s : names) {
            List<SystemArea> ares = systemAreaMapper.getAresByName(s);
            //匹配出来获取第一个结果（按parentid 倒序，第一个是子节点）
            if(ares != null && ares.size() > 0){
              lists.add(ares.get(0));
            }
		}
		return  lists;
	}
	
	 public Map<String, Object> getAreaNameAndNo(Integer areaNo){
		 Map<String, Object> nameNoMap = new HashMap<String, Object>();
		 //获取区
		 SystemArea district = systemAreaMapper.getSystemAreaByAreaNo(areaNo);
		 nameNoMap.put("districtNo", district.getAreaNo());
		 nameNoMap.put("districtName", district.getAreaName());
		 
		 //获取市
		 SystemArea city = systemAreaMapper.getSystemAreaByAreaNo(district.getParentAreaNo());
		 nameNoMap.put("cityNo", city.getAreaNo());
		 nameNoMap.put("cityName", city.getAreaName());
		 
		 //获取省
		 SystemArea province = systemAreaMapper.getSystemAreaByAreaNo(city.getParentAreaNo());
		 nameNoMap.put("provinceNo", province.getAreaNo());
		 nameNoMap.put("provinceName", province.getAreaName());
		 
		 return nameNoMap;
	 }
}
